/*
 * Copyright 2007 Johan Andries
 *
 * This file is released under the LGPL v2.1.
 * 
 * This file may be redistributed under the terms of the Library GNU Public
 * License.
 */

package rubymatic;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.Type;
import org.hibernate.usertype.UserCollectionType;
import org.jruby.Ruby;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.builtin.IRubyObject;


public class RubyArrayType implements UserCollectionType {
	
	
	public boolean contains(Object collection, Object entity) {
		
		IRubyObject rCollection = (IRubyObject)collection; 
		IRubyObject rObject = (IRubyObject)entity;
		
		Ruby runtime = ActiveHibernateUtil.getRubyInstance();
		return rCollection.callMethod(runtime.getCurrentContext(),"include?",
				new IRubyObject[] {rObject}).isTrue();
		
	}

	public Iterator getElementsIterator(Object collection) {
		final Ruby runtime = ActiveHibernateUtil.getRubyInstance();
		
		final IRubyObject rCollection = (IRubyObject)collection;
		IRubyObject rLength = rCollection.callMethod(runtime.getCurrentContext(), "length");
		final int length = ((Integer)JavaUtil.convertRubyToJava(rLength, Integer.TYPE)).intValue();
		
		return new Iterator() {
			private int index = 0;
			public boolean hasNext() {
				return index<length;
			}

			public Object next() {
				Object result = rCollection.callMethod(
						runtime.getCurrentContext(), "[]",
						new IRubyObject[] {JavaUtil.convertJavaToRuby(runtime, new Integer(index))});
				index++;
				return result;
			}

			public void remove() {
				throw new UnsupportedOperationException(
						"Iterator returned by getElementsIterator in JRubyArrayType");
			}
			
		};
	}

	public Object indexOf(Object collection, Object entity) {
		IRubyObject rCollection = (IRubyObject)collection; 
		IRubyObject rObject = (IRubyObject)entity;
				
		Ruby runtime = ActiveHibernateUtil.getRubyInstance();
		return rCollection.callMethod(runtime.getCurrentContext(),"index",
				new IRubyObject[] {rObject});
	}

	public PersistentCollection instantiate(SessionImplementor session,
			CollectionPersister persister) throws HibernateException {
		return newPersistentRubyArray(session);
	}
	
	private PersistentRubyArray newPersistentRubyArray(SessionImplementor session) {
		PersistentRubyArray prArray = (PersistentRubyArray)ActiveHibernateUtil
		.getRubyInstance().getModule("ActiveHibernate")
		.getClass("PersistentRubyArray").newInstance(new IRubyObject[0], null);
		prArray.setSession(session);
		return prArray;
	}
	
	public Object instantiate(int anticipatedSize) { 
		Ruby runtime = ActiveHibernateUtil.getRubyInstance();
		if(anticipatedSize>0) {
			return runtime.newArray(anticipatedSize);
		} else {
			return runtime.newArray();
		}
		
	}

	
	
	public Object replaceElements(Object original, Object target,
			CollectionPersister persister, Object owner, Map copyCache,
			SessionImplementor session) throws HibernateException {
		
		throw new RuntimeException("RubyArrayType.replaceElements not yet implemented");
	}

	public PersistentCollection wrap(SessionImplementor session,
			Object collection) {
		PersistentRubyArray prArray = newPersistentRubyArray(session);
		prArray.wrap((Collection)collection);
		return prArray;
	}

}
